<?php

/**
 * 销售数据
 * File Name：SalesDataModel.php
 * File Encoding：UTF-8
 * File New Time：2014-7-28 14:33:48
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class SalesDataModel extends BaseModel {

    public function tableName() {
        return '{{sales_data}}';
    }

    public function rules() {
        return array(
            //array('orders_number', 'unique', 'message' => '该订单编号已经存在！'),
            array('source,product_id,goods_id,item_type,brokerage_scale,orders_number,shop_name,nick,orders_create_time,sales,start_sales,price,promo_price,start_time,end_time,check_time,admin_id', 'safe'),
        );
    }

    //关联查询
    public function relations() {
        return array(
            'GoodsModel' => array(self::BELONGS_TO, 'GoodsModel', 'goods_id'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 根据excel导入数据
     * @param type $path excel文件路径
     * @return boolean
     */
    public function excelImport($path) {
        if (!file_exists($path)) {
            return false;
        }
        set_time_limit(0);
        ini_set('memory_limit', '256M');
        //require_once Yii::app()->basePath . '/extensions/Excel/reader.php';
        require_once Yii::app()->basePath . '/extensions/Excel/cvstoarray.php';
        $csv = new CvsToArray($path);
        $dataList = $csv->get_array();
        if (empty($dataList)) {
            return false;
        }
        $memcache = new Memcache();
        $memcache->connect('127.0.0.1', 11211);
        //$memcache->useMemcached = false;
        $memcache->set('SalesDataModel_excelImport', $dataList, 0, 600);
        return true;
    }

    /**
     * 数据导入
     * @param type $rs
     * @param type $source
     * @return boolean
     */
    public function import($rs, $source = 0) {
        if (empty($rs)) {
            return false;
        }
        if (empty($rs[10]) || empty($rs[12])) {
            return false;
        }
        $data = array(
            'source' => $source,
            'product_id' => $rs[10],
            'shop_name' => $rs[12],
            'nick' => $rs[11],
            'brokerage_scale' => number_format($rs[7], 5) / 100,
            'orders_number' => $rs[20],
            'orders_create_time' => strtotime($rs[0]),
            'start_sales' => 0,
            'sales' => $rs[2],
            'price' => number_format($rs[3], 3),
        );
        //本站存储的商品信息
        $goods = $this->getGoodsIndo($data['product_id'], $data['orders_create_time']);
        if (!empty($goods)) {
            $data['goods_id'] = $goods['goods_id'];
            $data['item_type'] = $goods['item_type'];
            $data['start_sales'] = $goods['start_sales'];
            $data['promo_price'] = $goods['promo_price'];
            $data['check_time'] = $goods['add_time'];
            $data['admin_id'] = $goods['admin_id'];
            $data['start_time'] = $goods['start_time'];
            $data['end_time'] = $goods['end_time'];
        } else {
            $data['goods_id'] = 0;
            $data['item_type'] = 0;
            $data['start_sales'] = 0;
            $data['promo_price'] = 0;
            $data['check_time'] = 0;
            $data['admin_id'] = 0;
            $data['start_time'] = 0;
            $data['end_time'] = 0;
        }
        unset($this->id);
        $this->attributes = $data;
        $this->setIsNewRecord(true);
        if ($this->validate()) {
            $this->save();
        } else {
            $info = $this->find($this->where(array('orders_number' => $data['orders_number'])));
            if (!empty($info)) {
                $info->attributes = $data;
                $info->save();
            }
        }
        return true;
    }

    /**
     * 获取本地对应报名信息
     * @param type $product_id
     * @param type $orders_create_time
     * @return type
     */
    public function getGoodsIndo($product_id, $orders_create_time) {
        if (empty($product_id) || empty($orders_create_time)) {
            return false;
        }
        $model = GoodsModel::model();
        $where = array(
            'product_id' => $product_id,
            'start_time' => array(array('EGT', $orders_create_time - 86400 * 3), array('ELT', $orders_create_time + 86400 * 3), 'and'),
            'order' => 'goods_id desc',
        );
        $criteria = $this->where($where);
        $data = $model->find($criteria);
        if (empty($data)) {
            return $data;
        }
        $goodsBm = GoodsBmModel::model()->findByPk($data->goodsbm_id);
        if (!empty($goodsBm)) {
            return array_merge($goodsBm->attributes, $data->attributes);
        }
        return $data->attributes;
    }

    /**
     * 删除指定日期数据
     * @return boolean
     */
    public function deleteAsTime() {
        $start_time = Yii::app()->request->getParam('start_time');
        if (empty($start_time)) {
            $this->addError('', '请输入要删除的日期');
            return FALSE;
        }
        $end_time = strtotime($start_time) + 86399;
        $where = array('orders_create_time' => array(array('EGT', strtotime($start_time)), array('ELT', $end_time), 'AND'));
        $criteria = $this->where($where);
        $data = $this->findAll($criteria);
        if (empty($data)) {
            $this->addError('', '没找到要删除的数据');
            return FALSE;
        }
        foreach ($data as $t) {
            $t->delete();
        }
        return true;
    }

}
